package db

import (
	"my-yipuku/config"
	"strconv"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// 定义全局变量

var (
	Db *gorm.DB
	Err error
)

func init() {
	dsn := config.UserName + ":" + config.PassWord + "@tcp("+ config.Host+ ":" + strconv.Itoa(config.Port) + ")/" + config.Database + "?charset=utf8"
	Db,Err = gorm.Open(mysql.Open(dsn),&gorm.Config{})
	if Err != nil {
	}
	if Db.Error != nil {
	}
	sqlDB, _ := Db.DB()
	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(10)

	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(100)

	// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
	sqlDB.SetConnMaxLifetime(time.Hour)
}